#!/usr/bin/env python
# -*- coding:utf-8 -*-
# !/usr/bin/env python
# -*- coding:utf-8 -*-

from django.core.exceptions import ValidationError
from django import forms as django_forms
from django.forms import fields as django_fields
from django.forms import widgets as django_widget
from django.forms import Form


class BaseForm(Form):
    def __init__(self, request, *args, **kwargs):
        self.request = request
        super(BaseForm, self).__init__(*args, **kwargs)


class LoginForm(BaseForm):
    username = django_fields.CharField(
        min_length=3,
        max_length=20,
        error_messages={'required': '用户名不能为空.', 'min_length': "用户名长度不能小于3个字符", 'max_length': "用户名长度不能大于20个字符"}
    )
    password = django_fields.CharField(error_messages={'required': '密码不能为空.'})
    # password = django_fields.RegexField(
    #     '^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$\%\^\&\*\(\)])[0-9a-zA-Z!@#$\%\^\&\*\(\)]{8,32}$',
    #     min_length=8,
    #     max_length=32,
    #     error_messages={'required': '密码不能为空.',
    #                     'invalid': '密码必须包含数字，字母、特殊字符',
    #                     'min_length': "密码长度不能小于8个字符",
    #                     'max_length': "密码长度不能大于32个字符"}
    # )

    rmb = django_fields.IntegerField(required=False)

    check_code = django_fields.CharField(
        error_messages={'required': '验证码不能为空.'}
    )

    def clean_check_code(self):
        if self.request.session.get('CheckCode').upper() != self.request.POST.get('check_code').upper():
            raise ValidationError(message='验证码错误', code='invalid')


class ChangePasswordForm(Form):
    """
    修改密码
    """
    password = django_fields.CharField(min_length=8,
                                       max_length=32,
                                       error_messages={
                                           'required': '密码不能为空.',
                                           'min_length': "密码长度不能小于8个字符",
                                           'max_length': "密码长度不能大于32个字符"
                                       },
                                       widget=django_widget.PasswordInput(attrs={'class': 'form-control'})
                                       )
    confirm_password = django_fields.CharField(min_length=8,
                                               max_length=32,
                                               error_messages={
                                                   'required': '确认密码不能为空.',
                                                   'min_length': "确认密码长度不能小于8个字符",
                                                   'max_length': "确认密码长度不能大于32个字符"
                                               },
                                               widget=django_widget.PasswordInput(attrs={'class': 'form-control'}))

    def clean_confirm_password(self):
        password = self.cleaned_data.get('password')
        confirm_password = self.cleaned_data.get('confirm_password')
        if password != confirm_password:
            raise ValidationError(message='密码不一致', code='invalid')
